import { defineStore } from 'pinia'

// 定义搜索相关的状态管理
interface SearchState {
  // 搜索关键词
  keyword: string
  // 搜索触发时间戳，用于强制重新搜索
  searchTime: number
  // 筛选参数
  filterParams: {
    filterType: string
    filterValue: string
    filterLabel: string
  } | null
}

export const useSearchStore = defineStore('search', {
  state: (): SearchState => ({
    keyword: '',
    searchTime: 0,
    filterParams: null
  }),

  actions: {
    // 设置搜索关键词
    setKeyword(keyword: string) {
      this.keyword = keyword
      this.searchTime = Date.now()
    },

    // 清除搜索关键词
    clearKeyword() {
      this.keyword = ''
      this.searchTime = 0
    },

    // 设置筛选参数
    setFilterParams(params: { filterType: string; filterValue: string; filterLabel: string }) {
      this.filterParams = params
      this.searchTime = Date.now()
    },

    // 清除筛选参数
    clearFilterParams() {
      this.filterParams = null
    }
  }
})
